import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
import Confirm from '#cpt/temp/Confirm'
import { MultipleType,CheckType,BoolStatus } from '@views/novel-coro-system/test-info/common/defaultData'
// ===================================================================== global declare
const { $fn, $Lazy, globalConfig } = window
const { COVIDUrl } = globalConfig
// ===================================================================== Lazy
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
const BatchAuditModal 		= $Lazy.state(()=>import('./batch-audit-modal'))
const PatientInfoModal		= $Lazy.state(()=>import('../../common/patient-info-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '创建时间',			names: ['create_at_start','create_at_end'], type:'date-range', name:'jz-time' },
			{ label: '报告单编号',		name: 'report_num' },
			{ label: '标本条码',			name: 'spec_code' },
			{ label: '医院',			name: 'hosp_id',	type: 'select', loading:true,	idStr:'uuid',	nameStr:'hospName' },
			{ label: '姓名',			name: 'patient_name' },
			{ label: '采集类型',			name: 'isMultiple',			type:'select',		data:MultipleType },
			{ label: '受检类型',			name: 'checkType',			type:'select',		data:CheckType },
		],
		formData:[
			{ label:'是否延用上次报告时间',	name: 'LastReportTime', 	type: 'radio', 	full:true, 	value:0, data:BoolStatus },
		],
		specCodes: []
	}
	
	model = {}
	permissions = {reportstate:1 }
	
	componentDidMount(){
		const { ban_btn } = $fn.local('permissions')
		if($fn.hasArray(ban_btn)){
			if(ban_btn.includes('caseinfo.reportstate')){		// 审核、召回
				this.permissions.reportstate = 0
			}
		}
		this.getSelect()
	}
	onTime = reset => {
		 $time.then(f=>{
			const arr = f.duration['2-9-now']()
			this.resetModel = { create_at_start: arr[0], create_at_end: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			this.fetch()
		})
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 医院
			this.hostData = await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} } )
			$fn.setRow(forms, 'hosp_id', this.hostData )
			this.setState({ forms })
			this.onTime()
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,COVIDUrl+'caseinfo/query',{param:{report_state:-2,status:1},COVID:true})
	
	// 表单
	cols = [
		{ title: '报告单编号',		dataIndex: 'reportNum', 		...$fn.table.spec_num },
		{ title: '标本条码', 		dataIndex: 'specCode', 			...$fn.table.spec_code },
		{ title: '姓名', 			dataIndex: 'patientName', 		...$fn.table.name  },
		{ title: '所属医院', 		dataIndex: 'hospId', 			width:200,				render: text => $fn.getState(this.hostData,text,'hospName','uuid') },
		{ title: '受检类型', 		dataIndex: 'checkType', 		width:100,	render: text => $fn.getState(CheckType,text) },
		{ title: '采集类型', 		dataIndex: 'isMultiple', 		width:100,	render: text => $fn.getState(MultipleType,text) },
		{ title: '创建时间', 		dataIndex: 'createdAt', 		...$fn.table.time },
		{ title: '采样时间', 		dataIndex: 'collTime', 			...$fn.table.time },
		TableControls.controls.call(this, { width: 100})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, reportNum, collTime, specCode }) => {
		return [
			TableControls.pushTo.call(this,'预览', `preview-report?report_num=${reportNum}`),
			Controls.click.call(this,'编辑',()=>this.patientInfoModal.open(uuid))
		]
	}
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows } = this.state
		let specArr = selectedRows.map(v=>v.specCode)		// 选中条码
		const uuids = selectedKeys.join(',')
		const reportNum = selectedRows.map(v=>v.reportNum).join(',')
		let specCodes = []		//	选中超过24小时条码
		if($fn.hasArray(selectedRows)){
			const nowDate = new Date().valueOf()
			selectedRows.forEach(v=>{
				const time = new Date(v.collTime).valueOf()
				if(nowDate - time > (24*3600000)){
					specCodes.push(v.specCode)
				}
			})
		}
		specCodes = Array.from(new Set(specCodes))
		specArr = Array.from(new Set(specArr))
		this.setState({specCodes})
		const {reportstate } = this.permissions
		return [
			Controls.pushTo.call(this,'预览', `preview-report?report_num=${reportNum}`, {isKeys:true}),
			(reportstate && !$fn.hasArray(specCodes)) && Confirm.call(this,'批量审核',COVIDUrl+'caseinfo/reportState',{isKeys:1,submit:{status:2,uuids}}),
			(reportstate && $fn.hasArray(specCodes)) && Controls.click.call(this,'批量审核',()=>this.bAudittRef.open(uuids,specArr),{isKeys:1}),
			reportstate && Controls.click.call(this,'批量召回', ()=>{
				this.submit = { uuids }
				this.formRef.setValue({LastReportTime:0})
				this.modal.open({formRef: this.formRef})
			},{isKeys:1}),
		]
	}
	render(){
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this,{ timeStr:'2-9-now' }) }
				{/* 表格 */}
				{ Table.call(this) }
				{/* 审核 */}
				<BatchAuditModal onRef={ref=>this.bAudittRef=ref} onRefresh = {()=>{
					this.setState({selectedKeys:[],selectedRows:[]})
					this.fetch()
				}} />
				{/* 召回 */}
				{FormModal.call(this,{ title:'召回', labelWidth:'s12', width:500, api:COVIDUrl+'caseinfo/reportStateCb',enter:false,
					onRefresh: ()=>this.setState({selectedKeys:[],selectedRows:[]})
				})}
				{/* 编辑 */}
				<PatientInfoModal onRef={ref => this.patientInfoModal = ref} onRefresh={ () => this.fetch() } />
			</>
		)
	}
}

export default Index